import re

from build_feature.extract_npm_different_version_functions.core.function_info import FunctionInfo


class JSAndTSFunctionInfo(FunctionInfo):
    def __init__(self, function_info):
        super().__init__(function_info)

    def get_signature(self):
        return self.get_name()

    @staticmethod
    def pre_handle_function_body(code: str):
        # 去除注释和空格回车干扰
        def remove_comments(code: str) -> str:
            """
            去除代码中的单行注释（//...）和多行注释（/* ... */）
            """
            # 移除单行注释
            code = re.sub(r'//.*', '', code)
            # 移除多行注释
            code = re.sub(r'/\*[\s\S]*?\*/', '', code)
            return code

        def remove_extra_spaces(code: str) -> str:
            """
            去除多余的空格和回车，合并多个空白字符为一个空格
            """
            # 去掉行首和行尾的空格、换行
            code = "\n".join(line.strip() for line in code.splitlines())
            # 将多个空白字符合并为一个空格
            code = re.sub(r'\s+', ' ', code)
            return code.strip()

        code = remove_comments(code)
        code = remove_extra_spaces(code)
        return code

    @staticmethod
    def is_effective_code_line_content(line_content: str) -> bool:
        if line_content is None:
            return False
        if line_content.isspace():
            return False
        trans_table = str.maketrans('', '', ' \t\n\r\f\v')
        line_content = line_content.translate(trans_table)
        if line_content in ['{', '}', "}else{", 'break', 'continue', ';', 'break;', 'continue;', 'return', '']:
            return False
        return True
    # getstring
    def __str__(self):
        return self.get_unique_key()